﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sharpCmd.EasterEggs
{
    public partial class SnakeForm : Form
    {
        public SnakeForm()
        {
            InitializeComponent();
            snake = new Snake();
            gameTimer.Start();
            buffer = new Bitmap(400, 400);
        }

        Snake snake;
        Bitmap buffer;

        private void Snake_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Left)
            {
                snake.Direction = Snake.DirectionEnum.Left;
            }
            else if (e.KeyCode == Keys.Right)
            {
                snake.Direction = Snake.DirectionEnum.Right;
            }
            else if (e.KeyCode == Keys.X)
            {

            }
        }

        private void Snake_KeyUp(object sender, KeyEventArgs e)
        {
            snake.Direction = Snake.DirectionEnum.None;
        }
       

        private void gameTimer_Tick(object sender, EventArgs e)
        {
            //move snake
            if (!snake.Move())
            {
                //collision
                gameTimer.Stop();
                MessageBox.Show("Boom!","GAME OVER");
                return;
            }
            labelLength.Text = snake.points.Count.ToString();
            
            //redraw stuff
            using (Graphics g = Graphics.FromImage(buffer))
            {
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                g.Clear(Color.Black);
                foreach (var point in snake.points)
                {
                    g.FillRectangle(Brushes.Green, point.X - 1, point.Y - 1, 3, 3);
                }
            }
            pictureBox1.Image = buffer;
        }
    }
}
